#!/bin/bash

echo "开始在新服务器上部署..."

# 更新系统
echo "更新软件包..."
apt update

# 安装必要的软件包
echo "安装Python环境..."
apt install -y python3 python3-pip python3-venv nginx supervisor

echo "安装数据库..."
apt install -y mysql-server

# 创建项目目录
echo "创建项目目录..."
mkdir -p /home/www
cd /home/www

# 解压部署包
echo "解压部署包..."
tar -xzf /tmp/venue_booking_backend_deploy_*.tar.gz
mv venue_booking_backend_deploy venue_booking_system

cd venue_booking_system

# 创建Python虚拟环境
echo "创建Python虚拟环境..."
python3 -m venv venv
source venv/bin/activate

# 安装Python依赖
echo "安装Python依赖..."
pip install --upgrade pip
pip install -r requirements.txt

# 创建配置文件
echo "创建配置文件..."
cp env_template_new_server.txt .env

# 配置MySQL数据库
echo "配置MySQL数据库..."
mysql -e "CREATE DATABASE IF NOT EXISTS venue_booking_db;" 2>/dev/null || true
mysql -e "CREATE USER IF NOT EXISTS 'venue_user'@'localhost' IDENTIFIED BY 'venue_password';" 2>/dev/null || true
mysql -e "GRANT ALL PRIVILEGES ON venue_booking_db.* TO 'venue_user'@'localhost';" 2>/dev/null || true
mysql -e "FLUSH PRIVILEGES;" 2>/dev/null || true

# 初始化数据库
echo "初始化数据库..."
source venv/bin/activate
python -c "
from app import create_app
from app.extensions import db
app = create_app()
with app.app_context():
    db.create_all()
    print('数据库表创建完成')
" 2>/dev/null || echo "数据库初始化可能需要手动配置"

# 配置Nginx
echo "配置Nginx..."
cat > /etc/nginx/sites-available/venue_booking << 'EOF'
server {
    listen 80;
    server_name 1.92.133.195;

    client_max_body_size 16M;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /uploads {
        alias /home/www/venue_booking_system/uploads;
        expires 30d;
        add_header Cache-Control public;
    }

    location /static {
        alias /home/www/venue_booking_system/static;
        expires 30d;
        add_header Cache-Control public;
    }
}
EOF

ln -sf /etc/nginx/sites-available/venue_booking /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl restart nginx

# 配置Supervisor
echo "配置Supervisor..."
cat > /etc/supervisor/conf.d/venue_booking.conf << 'EOF'
[program:venue_booking]
command=/home/www/venue_booking_system/venv/bin/gunicorn -c gunicorn.conf.py run:app
directory=/home/www/venue_booking_system
user=www-data
autostart=true
autorestart=true
stdout_logfile=/home/www/venue_booking_system/logs/gunicorn.log
stderr_logfile=/home/www/venue_booking_system/logs/gunicorn_error.log
EOF

mkdir -p /home/www/venue_booking_system/logs
chown -R www-data:www-data /home/www/venue_booking_system

supervisorctl reread
supervisorctl update
supervisorctl start venue_booking

echo "部署完成！"
echo "访问地址: http://1.92.133.195"
echo "API文档: http://1.92.133.195/docs"
echo ""
echo "查看服务状态："
echo "supervisorctl status venue_booking"
echo ""
echo "查看日志："
echo "tail -f /home/www/venue_booking_system/logs/gunicorn.log" 